package com.jackrain.nea.core.mapper;

import com.jackrain.nea.core.entity.AdTableSqlModel;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;

@Mapper
@Component
public interface AdTableSqlMapper {

    @Delete("delete from ad_tablesql where ad_table_id = #{tableId}")
    int delete(long tableId);

    @Select("select sql_drop sqlDrop,SQL_CREATE sqlCreate from ad_tablesql where id = #{tableSqlId}")
    AdTableSqlModel select(long tableSqlId);

    @Select("select sql_drop from ad_tablesql where id = #{tableSqlId}")
    String sqlDrop(long tableSqlId);

    @Select("select SQL_CREATE from ad_tablesql where id = #{tableSqlId}")
    String sqlCreate(long tableSqlId);

    @Update("UPDATE ad_tablesql SET SQL_LOG = NULL WHERE ID = #{tableSqlId}")
    int updateSqlLog2Null(long tableSqlId);

    @Update("UPDATE ad_tablesql SET sqlaction = NULL WHERE ID = #{tableSqlId}")
    int updateSqlAction2Null(long tableSqlId);

    @Update("UPDATE ad_tablesql set sql_log = concat(#{sql},';', CHAR(10),ifnull(sql_log,' ')) where ad_table_id = #{tableid}")
    int DDLLog(@Param("tableid") long tableid, @Param("sql") String sql);

    @Update("update ad_tablesql set SQL_CREATE = #{create},sql_drop = #{drop} where id = #{id}")
    int updateCreateAndDrop(@Param("id") long id, @Param("create") String create, @Param("drop") String drop);

    @Insert("insert into AD_TABLESQL(ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE, "
            + "AD_TABLE_ID, CREATIONDATE,MODIFIEDDATE, OWNERID, MODIFIERID)"
            + "select #{id}, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,"
            + "#{newTableId}, #{now}, #{now}, OWNERID, MODIFIERID "
            + "from ad_table where id = #{srcTableId}")
    int clone(@Param("id") long id, @Param("srcTableId") long srcTableId, @Param("newTableId") long newTableId, @Param("now") Timestamp now);
}
